import { dwebServiceWorker, windowPlugin } from "@plaoc/plugins";
import { PromiseOut } from "@bnqkl/util-web/extends-promise-out";

// 定义常量
const $WALLET_PLAOC_PATH = {
  getAddress: '/wallet/authorize/address',
  signature: '/wallet/authorize/signature',
};

const $WALLET_SIGNATURE_TYPE = {
  message: 'message',
  transfer: 'transfer',
  bioforestChainCertificateTransfer: 'bioforestChainCertificateTransfer',
  json: 'json',
  ENTITY: 'ENTITY',
  assetTypeBalance: 'assetTypeBalance',
  contract: 'contract',
};

/**​ 是否有安装应用 */
const canOpenId = (id) => dwebServiceWorker.has(id);

/**​ 窗口最大化
 *  该api正常是项目启动的时候调用，不然应用是窗口模式
 */
const appMaximize = () => windowPlugin.maximize();

/**​ 应用聚焦 */
const focusWindow = () => windowPlugin.focusWindow();

/**​ 重启 */
const restart = () => dwebServiceWorker.restart();

/**​ 调用对应biw钱包获取相对应数据 */
const getBIWMetaAppData = (id, pathname, search) => {
  let signaturedata = '';
  let activate = true; // 是否激活对方应用界面
  const url = new URL(pathname, `file://${id}`);
  if (search) {
    if (pathname === $WALLET_PLAOC_PATH.signature) {
      signaturedata = JSON.stringify(search);
      if (search && search.length === 1) {
        const item = search[0];
        if (item.type === $WALLET_SIGNATURE_TYPE.assetTypeBalance) {
          activate = false;
        }
      }
    } else {
      for (const key in search) {
        url.searchParams.set(key, search[key]);
      }
    }
  }
  const init = {
    activate, // 是否激活对方应用界面
    method: "POST",
    body: signaturedata,
  };
  const promiseOut = new PromiseOut();
  dwebServiceWorker.fetch(url.href, init)
    .then(async (res) => {
      if (res.ok) {
        const dataJson = await res.json();
        const data = dataJson.data;
        if (data) {
          // 有返回，排查下有没抛错的情况
          const errorItem = data.find((item) => {
            if (item && typeof item === "object" && 'error' in item) {
              if (item.error) {
                return true;
              }
              return false;
            }
            return false;
          });
          if (errorItem) {
            promiseOut.reject(new Error(errorItem.message));
            throw new Error(errorItem.message);
          }
        }
        promiseOut.resolve(data);
        return res;
      } else {
        const error = await res.text();
        promiseOut.reject(new Error(error));
        return;
      }
    })
    .catch((err) => {
      promiseOut.reject(err);
    });
  return {
    getData: () => {
      return promiseOut.promise;
    },
    abort: () => {
      return promiseOut.reject(new Error("request abort"));
    },
  };
};

export default {
  canOpenId,
  appMaximize,
  restart,
  getBIWMetaAppData,
  focusWindow,
};